package com.portico.security

class User {

  transient springSecurityService

	String username
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true,  email:true
		password blank: false
	}

	static mapping = {
        table name:"app_user"
		password column:"user_password"
	}

	Set<Authority> getAuthorities() {
		UserAuthority.findAllByPerson(this).collect { it.authority } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}

    String encodePassword(String pwd) {
          springSecurityService.encodePassword(pwd)
    }
  
  String toString() {
    username
  }
}
